package com.txby.platformApi.component.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jck.mybatis.log.GlobalLogAnnotation;
import com.jck.mybatis.util.BeanToMapUtil;
import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.pf.modules.api.annotation.AuthIgnore;
import com.txby.platformCore.data.CommonApiModelMapExt;
import com.txby.platformCore.entity.DoctorAdvice;
import com.txby.platformCore.entity.SickBaseInfo;
import com.txby.platformCore.entity.TxbyBDApiConfig;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 患者信息
* @author 作者 jacking
* @date  
 */
@SuppressWarnings("all")
@RestController
@Api(value="患者信息",description="患者信息-说明")
@RequestMapping("/sickInfo")
public class SickInfoController extends CommonController{
	private static Logger log = Logger.getLogger(SickInfoController.class);

	@ApiOperation(value = "查询住院出院时间列表", notes = "查询住院出院时间列表-说明")
	@PostMapping("/getHosPeriodInfo/{type}/{token}")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",dataType="number",name="type",value="请求端（ 0:web,1:android,2:iphone）",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="token",value="用户令牌",required=true),
	})
	@GlobalLogAnnotation(logType=2,discription="查询住院出院时间列表")
	public Map getHosPeriodInfo(@PathVariable("type") Integer type, @PathVariable("token") String token,SickBaseInfo sickBaseInfo ) {
		CommonApiModelMapExt mm = chekTokenCahceValid(token, type);
		try {
			if (!isTokenValid) {
				return mm;
			}
			if (sickBaseInfo != null) {
				this.mBaseDao.saveObject(sickBaseInfo);
				mm.put(CommonApiModelMapExt.ERRCODE, 0);
				mm.put(CommonApiModelMapExt.ERRMSG, "保存成功");
				return mm;
			} else {
				mm.put(CommonApiModelMapExt.ERRCODE, 0);
				mm.put(CommonApiModelMapExt.ERRMSG, "保存成功");
				return mm;
			} 
		} catch (Exception e) {
			e.printStackTrace();
			mm.put(CommonApiModelMapExt.ERRCODE, 1);
			mm.put(CommonApiModelMapExt.ERRMSG, "系统异常");
		}
		return mm;
	}
	
	@ApiOperation(value = "保存患者基本信息", notes = "保存患者基本信息-说明")
	@PostMapping("/saveSickBaseInfo/{type}/{token}")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",dataType="number",name="type",value="请求端（ 0:web,1:android,2:iphone）",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="token",value="用户令牌",required=true)
	})
	@GlobalLogAnnotation(logType=2,discription="保存患者基本信息")
	public Map saveSickBaseInfo(@PathVariable("type") Integer type, @PathVariable("token") String token,SickBaseInfo sickBaseInfo ) {
		CommonApiModelMapExt mm = chekTokenCahceValid(token, type);
		try {
			if (!isTokenValid) {
				return mm;
			}
			if (sickBaseInfo != null) {
				this.mBaseDao.saveObject(sickBaseInfo);
				mm.put(CommonApiModelMapExt.ERRCODE, 0);
				mm.put(CommonApiModelMapExt.ERRMSG, "保存成功");
				return mm;
			} else {
				mm.put(CommonApiModelMapExt.ERRCODE, 0);
				mm.put(CommonApiModelMapExt.ERRMSG, "保存成功");
				return mm;
			} 
		} catch (Exception e) {
			e.printStackTrace();
			mm.put(CommonApiModelMapExt.ERRCODE, 1);
			mm.put(CommonApiModelMapExt.ERRMSG, "系统异常");
		}
		return mm;
	}
	@ApiOperation(value = "医护医嘱护士执行", notes = "医护医嘱护士执行-说明")
	@PostMapping("/callOrdersInfo/{type}/{token}")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",dataType="number",name="type",value="请求端（ 0:web,1:android,2:iphone）",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="token",value="用户令牌",required=true)
	})
	@GlobalLogAnnotation(logType=2,discription="医护医嘱护士执行")
	public Map callOrdersInfo(@PathVariable("type") Integer type, @PathVariable("token") String token,SickBaseInfo sickBaseInfo ) {
		CommonApiModelMapExt mm = chekTokenCahceValid(token, type);
		try {
			if (!isTokenValid) {
				return mm;
			}
			if (sickBaseInfo != null) {
				this.mBaseDao.saveObject(sickBaseInfo);
				mm.put(CommonApiModelMapExt.ERRCODE, 0);
				mm.put(CommonApiModelMapExt.ERRMSG, "保存成功");
				return mm;
			} else {
				mm.put(CommonApiModelMapExt.ERRCODE, 0);
				mm.put(CommonApiModelMapExt.ERRMSG, "保存成功");
				return mm;
			} 
		} catch (Exception e) {
			e.printStackTrace();
			mm.put(CommonApiModelMapExt.ERRCODE, 1);
			mm.put(CommonApiModelMapExt.ERRMSG, "系统异常");
		}
		return mm;
	}
	
	@ApiOperation(value = "患者用户用药时间或康复活动提醒执行", notes = "患者用户用药时间或康复活动提醒执行-说明")
	@PostMapping("/callSickRemindMedicineAndActivity/{type}/{token}")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",dataType="number",name="type",value="请求端（ 0:web,1:android,2:iphone）",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="token",value="用户令牌",required=true)
	})
	@GlobalLogAnnotation(logType=2,discription="患者用户用药时间或康复活动提醒执行")
	public Map callSickRemindMedicineAndActivity(@PathVariable("type") Integer type, @PathVariable("token") String token,SickBaseInfo sickBaseInfo ) {
		CommonApiModelMapExt mm = chekTokenCahceValid(token, type);
		try {
			if (!isTokenValid) {
				return mm;
			}
			if (sickBaseInfo != null) {
				this.mBaseDao.saveObject(sickBaseInfo);
				mm.put(CommonApiModelMapExt.ERRCODE, 0);
				mm.put(CommonApiModelMapExt.ERRMSG, "保存成功");
				return mm;
			} else {
				mm.put(CommonApiModelMapExt.ERRCODE, 0);
				mm.put(CommonApiModelMapExt.ERRMSG, "保存成功");
				return mm;
			} 
		} catch (Exception e) {
			e.printStackTrace();
			mm.put(CommonApiModelMapExt.ERRCODE, 1);
			mm.put(CommonApiModelMapExt.ERRMSG, "系统异常");
		}
		return mm;
	}
	
	
	
	@ApiOperation(value = "编辑患者基本信息", notes = "编辑患者基本信息-说明")
	@PostMapping("/editSickBaseInfo/{type}/{token}")
	@ApiParam(hidden=true)
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",dataType="number",name="type",value="请求端（ 0:web,1:android,2:iphone）",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="token",value="用户令牌",required=true)
		
		})
	@GlobalLogAnnotation(logType=2,discription="编辑患者基本信息")
	public Map editSickBaseInfo(@PathVariable("type") Integer type, @PathVariable("token") String token,SickBaseInfo sickBaseInfo ) {
		CommonApiModelMapExt mm = chekTokenCahceValid(token, type);
		try {
			if (!isTokenValid) {
				return mm;
			}
			if (sickBaseInfo != null) {
				Integer result=0;
				if(!ChkUtil.isEmpty(sickBaseInfo.getId())){
					sickBaseInfo.setUpdateTime(new Date());
					this.mBaseDao.updateObject(sickBaseInfo);
				}else{
					
					this.mBaseDao.saveObject(sickBaseInfo);
				}
			    if(result>0){
			    	mm.put(CommonApiModelMapExt.ERRCODE, 0);
					mm.put(CommonApiModelMapExt.ERRMSG, "保存成功");
			    }
				return mm;
			} else {
				mm.put(CommonApiModelMapExt.ERRCODE, 1);
				mm.put(CommonApiModelMapExt.ERRMSG, "参数有误");
				return mm;
			} 
		} catch (Exception e) {
			e.printStackTrace();
			mm.put(CommonApiModelMapExt.ERRCODE, 1);
			mm.put(CommonApiModelMapExt.ERRMSG, "系统异常");
		}
		return mm;
	}
	
	@ApiOperation(value = "查询患者基本信息", notes = "查询患者基本信息-说明")
	@GetMapping("/getSickBaseInfo/{type}/{token}")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",dataType="number",name="type",value="请求端（ 0:web,1:android,2:iphone）",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="token",value="用户令牌",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="bedNO",value="患者床号",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="sickAreaNO",value="床位病区号",required=true)
	})
	@GlobalLogAnnotation(logType=2,discription="查询患者基本信息患者")
	public Map getSickBaseInfo(@PathVariable("type") Integer type, @PathVariable("token") String token,@RequestParam(required=true,name="bedNO")String bedNO,@RequestParam(required=true,name="sickAreaNO")String sickAreaNO) {
		CommonApiModelMapExt mm = chekTokenCahceValid(token, type);
		try {
			if (!isTokenValid) {
				return mm;
			}
			 
			Map paramMap = new HashMap<>();
			String searchSql = "";
			if (!ChkUtil.isEmpty(bedNO)) {
					searchSql += " and bedNO = #{pojo.bedNO} ";
					paramMap.put("bedNO",  bedNO);
			}
			if (!ChkUtil.isEmpty(sickAreaNO)) {
				searchSql += " and sickAreaNO = #{pojo.sickAreaNO} ";
				paramMap.put("sickAreaNO",  sickAreaNO);
			}
			
//			searchSql += " order by inHosTime desc";//默认入院时间倒序
			PageList pl=new PageList<>();
//			查询job接口是否异常， 如果异常 则只取本地数据库
			this.isHisErr(TxbyBDApiConfig.class.getName());
		     List<SickBaseInfo> oInfoList=new ArrayList<>();
			if(propertySourceConfig.getIsExecuteHisHttp()!=null&&propertySourceConfig.getIsExecuteHisHttp().equals("true")&&!isHisErr){
				List<TxbyBDApiConfig> bdInfoList=this.getTxbyBDApiConfigInfo(TxbyBDApiConfig.class.getName());
				if(!ChkUtil.isEmptyAllObject(bdInfoList)){
					TxbyBDApiConfig bdInfo=bdInfoList.get(0);
					Map jo=JSON.parseObject(bdInfo.getApiParam());
					jo.put("bedNO", bedNO);
					jo.put("sickNO", bedNO);
//					{"bedNO": "$bedNO", "sickNO": "$sickNO"}
				    String json=this.getHttpResponseContent(bdInfo.getApiBaseUrl()+bdInfo.getApiServletPath(), "get",jo);
				    JSONObject jsonData=JSON.parseObject(json);
				     CommonApiModelMapExt temp= new CommonApiModelMapExt();
				     BeanUtils.copyProperties(temp, jsonData);
				
				    if(temp.getCode()==1){
				    	Map dataO=new HashMap();
				    	SickBaseInfo oInfo=new SickBaseInfo();
				    	Object o=jsonData.get("data");
                        if(!(o instanceof JSONArray)){
                        	BeanUtils.copyProperties(oInfo, o);
                        	oInfoList.add(oInfo);
                        }else{
                        	List oList=(List) o;
                        	for(Object obj:oList){
                        		oInfo=new SickBaseInfo();
                    			BeanUtils.copyProperties(oInfo, obj);
                    			oInfoList.add(oInfo);
                        	}
                        	
                        	//保存数组
                        }
				    	
				    }
				    
//				    循环处理
				    for(SickBaseInfo sbi:oInfoList){
				    	List<SickBaseInfo> oldObj=this.mBaseDao.findBySql("select * from  "+mBaseDao.tableName(SickBaseInfo.class)+" where 1=1   "+searchSql, SickBaseInfo.class, paramMap);
				    	SickBaseInfo oInfo=new SickBaseInfo();
				    	if(oldObj.size()>0){
				    		oInfo= oldObj.get(0);
				    		String id=oInfo.getId();
				    		BeanUtils.copyProperties(oInfo, sbi);
				    		oInfo.setId(id);
				    		oInfo.setUpdateTime(new Date());
				    		this.mBaseDao.updateObject(oInfo);
				    	}else{
				    		this.mBaseDao.saveObject(sbi);
				    	}
				    }
					
				}
				pl =new PageList<>(oInfoList, null);
			}else{
				
				pl = this.mBaseDao.findBySqlForPageList("select * from  "+mBaseDao.tableName(SickBaseInfo.class)+" where 1=1  "+searchSql, Map.class, null, paramMap);
			}
			/**
			 * 请求http数据api
			 */
			
			mm.put(CommonApiModelMapExt.ERRCODE, 0);
			mm.put(CommonApiModelMapExt.ERRMSG, "查询成功");
			mm.put("data", pl.getResultList());
			return mm;
			
		} catch (Exception e) {
			e.printStackTrace();
			mm.put(CommonApiModelMapExt.ERRCODE, 1);
			mm.put(CommonApiModelMapExt.ERRMSG, "系统异常");
		}
		

		return mm;
	}
	@ApiOperation(value = "查询患者详细信息", notes = "查询患者详细信息-说明")
	@GetMapping("/getSickDetailInfo/{type}/{token}")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",dataType="number",name="type",value="请求端（ 0:web,1:android,2:iphone）",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="token",value="用户令牌",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="bedNO",value="患者床号",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="sickAreaNO",value="床位病区号",required=true)
	})
	@GlobalLogAnnotation(logType=2,discription="查询患者详细信息")
	public Map getSickDetailInfo(@PathVariable("type") Integer type, @PathVariable("token") String token,@RequestParam(required=true,name="bedNO")String bedNO,@RequestParam(required=true,name="sickAreaNO")String sickAreaNO) {
		CommonApiModelMapExt mm = chekTokenCahceValid(token, type);
		try {
			if (!isTokenValid) {
				return mm;
			}
			
			Map paramMap = new HashMap<>();
			String searchSql = "";
			if (!ChkUtil.isEmpty(bedNO)) {
				searchSql += " and bedNO = #{pojo.bedNO} ";
				paramMap.put("bedNO",  bedNO);
			}
			if (!ChkUtil.isEmpty(sickAreaNO)) {
				searchSql += " and sickAreaNO = #{pojo.sickAreaNO} ";
				paramMap.put("sickAreaNO",  sickAreaNO);
			}
			
//			searchSql += " order by inHosTime desc";//默认入院时间倒序
			PageList pl=new PageList<>();
//			查询job接口是否异常， 如果异常 则只取本地数据库
			this.isHisErr(TxbyBDApiConfig.class.getName());
			List<SickBaseInfo> oInfoList=new ArrayList<>();
			if(propertySourceConfig.getIsExecuteHisHttp()!=null&&propertySourceConfig.getIsExecuteHisHttp().equals("true")&&!isHisErr){
				List<TxbyBDApiConfig> bdInfoList=this.getTxbyBDApiConfigInfo(TxbyBDApiConfig.class.getName());
				if(!ChkUtil.isEmptyAllObject(bdInfoList)){
					TxbyBDApiConfig bdInfo=bdInfoList.get(0);
					Map jo=JSON.parseObject(bdInfo.getApiParam());
					jo.put("bedNO", bedNO);
					jo.put("sickNO", bedNO);
//					{"bedNO": "$bedNO", "sickNO": "$sickNO"}
					String json=this.getHttpResponseContent(bdInfo.getApiBaseUrl()+bdInfo.getApiServletPath(), "get",jo);
					JSONObject jsonData=JSON.parseObject(json);
					CommonApiModelMapExt temp= new CommonApiModelMapExt();
					BeanUtils.copyProperties(temp, jsonData);
					
					if(temp.getCode()==1){
						Map dataO=new HashMap();
						SickBaseInfo oInfo=new SickBaseInfo();
						Object o=jsonData.get("data");
						if(!(o instanceof JSONArray)){
							BeanUtils.copyProperties(oInfo, o);
							oInfoList.add(oInfo);
						}else{
							List oList=(List) o;
							for(Object obj:oList){
								oInfo=new SickBaseInfo();
								BeanUtils.copyProperties(oInfo, obj);
								oInfoList.add(oInfo);
							}
							
							//保存数组
						}
						
					}
					
//				    循环处理
					for(SickBaseInfo sbi:oInfoList){
						List<SickBaseInfo> oldObj=this.mBaseDao.findBySql("select * from  "+mBaseDao.tableName(SickBaseInfo.class)+" where 1=1   "+searchSql, SickBaseInfo.class, paramMap);
						SickBaseInfo oInfo=new SickBaseInfo();
						if(oldObj.size()>0){
							oInfo= oldObj.get(0);
							String id=oInfo.getId();
							BeanUtils.copyProperties(oInfo, sbi);
							oInfo.setId(id);
							oInfo.setUpdateTime(new Date());
							this.mBaseDao.updateObject(oInfo);
						}else{
							this.mBaseDao.saveObject(sbi);
						}
					}
					
				}
				pl =new PageList<>(oInfoList, null);
			}else{
				
				pl = this.mBaseDao.findBySqlForPageList("select * from  "+mBaseDao.tableName(SickBaseInfo.class)+" where 1=1  "+searchSql, Map.class, null, paramMap);
			}
			/**
			 * 请求http数据api
			 */
			
			mm.put(CommonApiModelMapExt.ERRCODE, 0);
			mm.put(CommonApiModelMapExt.ERRMSG, "查询成功");
			mm.put("data", pl.getResultList());
			return mm;
			
		} catch (Exception e) {
			e.printStackTrace();
			mm.put(CommonApiModelMapExt.ERRCODE, 1);
			mm.put(CommonApiModelMapExt.ERRMSG, "系统异常");
		}
		
		return mm;
	}
	@ApiOperation(value = "查询患者医护的医嘱查询信息", notes = "查询患者医护的医嘱查询信息-说明")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",dataType="number",name="type",value="请求端（ 0:web,1:android,2:iphone）",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="token",value="用户令牌",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="inHosNO",value="住院号",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="orderTime",value="医嘱日期（默认今日）yyyy-MM-dd",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="isValid",value="是否有效",required=true)
	})
	@GetMapping("/getOrdersInfo/{type}/{token}")
	@GlobalLogAnnotation(logType=2,discription="查询患者医护的医嘱查询信息")
	public Map getOrdersInfo(@PathVariable("type") Integer type, @PathVariable("token") String token,@RequestParam(required=true,name="inHosNO")String inHosNO,
			@RequestParam(required=true,name="orderTime") String orderTime,@RequestParam(required=true,name="isValid") String isValid) {
		CommonApiModelMapExt mm = chekTokenCahceValid(token, type);
		try {
			if (!isTokenValid) {
				return mm;
			}
			
			Map paramMap = new HashMap<>();
			String searchSql = "";
			if (!ChkUtil.isEmpty(inHosNO)) {
				searchSql += " and inHosNO = #{pojo.inHosNO} ";
				paramMap.put("inHosNO",  inHosNO);
			}
			if (!ChkUtil.isEmpty(orderTime)) {
				searchSql += " and orderTime = #{pojo.orderTime} ";
				paramMap.put("orderTime",  orderTime);
			}
			if (!ChkUtil.isEmpty(isValid)) {
				searchSql += " and isValid = #{pojo.isValid} ";
				paramMap.put("isValid",  isValid);
			}
			
//			searchSql += " order by inHosTime desc";//默认入院时间倒序
			PageList pl=new PageList<>();
//			查询job接口是否异常， 如果异常 则只取本地数据库
			this.isHisErr(TxbyBDApiConfig.class.getName());
			List<DoctorAdvice> oInfoList=new ArrayList<>();
			if(propertySourceConfig.getIsExecuteHisHttp()!=null&&propertySourceConfig.getIsExecuteHisHttp().equals("true")&&!isHisErr){
				List<TxbyBDApiConfig> bdInfoList=this.getTxbyBDApiConfigInfo(TxbyBDApiConfig.class.getName());
				if(!ChkUtil.isEmptyAllObject(bdInfoList)){
					TxbyBDApiConfig bdInfo=bdInfoList.get(0);
					Map jo=JSON.parseObject(bdInfo.getApiParam());
					jo.put("inHosNO", inHosNO);
					jo.put("orderTime", orderTime);
//					{"bedNO": "$bedNO", "sickNO": "$sickNO"}
					String json=this.getHttpResponseContent(bdInfo.getApiBaseUrl()+bdInfo.getApiServletPath(), "get",jo);
					JSONObject jsonData=JSON.parseObject(json);
					CommonApiModelMapExt temp= new CommonApiModelMapExt();
					BeanUtils.copyProperties(temp, jsonData);
					
					if(temp.getCode()==1){
						Map dataO=new HashMap();
						DoctorAdvice oInfo=new DoctorAdvice();
						Object o=jsonData.get("data");
						if(!(o instanceof JSONArray)){
							BeanUtils.copyProperties(oInfo, o);
							oInfoList.add(oInfo);
						}else{
							List oList=(List) o;
							for(Object obj:oList){
								oInfo=new DoctorAdvice();
								BeanUtils.copyProperties(oInfo, obj);
								oInfoList.add(oInfo);
							}
							
							//保存数组
						}
						
					}
					
//				    循环处理
					for(DoctorAdvice sbi:oInfoList){
						List<DoctorAdvice> oldObj=this.mBaseDao.findBySql("select * from  "+mBaseDao.tableName(DoctorAdvice.class)+" where 1=1   "+searchSql, DoctorAdvice.class, paramMap);
						DoctorAdvice oInfo=new DoctorAdvice();
						if(oldObj.size()>0){
							oInfo= oldObj.get(0);
							String id=oInfo.getId();
							BeanUtils.copyProperties(oInfo, sbi);
							oInfo.setId(id);
							oInfo.setUpdateTime(new Date());
							this.mBaseDao.updateObject(oInfo);
						}else{
							this.mBaseDao.saveObject(sbi);
						}
					}
					
				}
				pl =new PageList<>(oInfoList, null);
			}else{
				
				pl = this.mBaseDao.findBySqlForPageList("select * from  "+mBaseDao.tableName(DoctorAdvice.class)+" where 1=1  "+searchSql, Map.class, null, paramMap);
			}
			/**
			 * 请求http数据api
			 */
			
			mm.put(CommonApiModelMapExt.ERRCODE, 0);
			mm.put(CommonApiModelMapExt.ERRMSG, "查询成功");
			mm.put("data", pl.getResultList());
			return mm;
			
		} catch (Exception e) {
			e.printStackTrace();
			mm.put(CommonApiModelMapExt.ERRCODE, 1);
			mm.put(CommonApiModelMapExt.ERRMSG, "系统异常");
		}
		
		return mm;
	}
	@ApiOperation(value = "查询患者的检验信息", notes = "查询患者的检验信息-说明")
	@GetMapping("/getSickCheckResultInfo/{type}/{token}")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",dataType="number",name="type",value="请求端（ 0:web,1:android,2:iphone）",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="token",value="用户令牌",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="bedNO",value="患者床号",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="sickAreaNO",value="床位病区号",required=true)
	})
	@GlobalLogAnnotation(logType=2,discription="查询患者的检验信息")
	public Map getSickCheckResultInfo(@PathVariable("type") Integer type, @PathVariable("token") String token,@RequestParam(required=true,name="bedNO")String bedNO,@RequestParam(required=true,name="sickAreaNO")String sickAreaNO) {
		CommonApiModelMapExt mm = chekTokenCahceValid(token, type);
		try {
			if (!isTokenValid) {
				return mm;
			}
			
			Map paramMap = new HashMap<>();
			String searchSql = "";
			if (!ChkUtil.isEmpty(bedNO)) {
				searchSql += " and bedNO = #{pojo.bedNO} ";
				paramMap.put("bedNO",  bedNO);
			}
			if (!ChkUtil.isEmpty(sickAreaNO)) {
				searchSql += " and sickAreaNO = #{pojo.sickAreaNO} ";
				paramMap.put("sickAreaNO",  sickAreaNO);
			}
			
//			searchSql += " order by inHosTime desc";//默认入院时间倒序
			PageList pl=new PageList<>();
//			查询job接口是否异常， 如果异常 则只取本地数据库
			this.isHisErr(TxbyBDApiConfig.class.getName());
			List<SickBaseInfo> oInfoList=new ArrayList<>();
			if(propertySourceConfig.getIsExecuteHisHttp()!=null&&propertySourceConfig.getIsExecuteHisHttp().equals("true")&&!isHisErr){
				List<TxbyBDApiConfig> bdInfoList=this.getTxbyBDApiConfigInfo(TxbyBDApiConfig.class.getName());
				if(!ChkUtil.isEmptyAllObject(bdInfoList)){
					TxbyBDApiConfig bdInfo=bdInfoList.get(0);
					Map jo=JSON.parseObject(bdInfo.getApiParam());
					jo.put("bedNO", bedNO);
					jo.put("sickNO", bedNO);
//					{"bedNO": "$bedNO", "sickNO": "$sickNO"}
					String json=this.getHttpResponseContent(bdInfo.getApiBaseUrl()+bdInfo.getApiServletPath(), "get",jo);
					JSONObject jsonData=JSON.parseObject(json);
					CommonApiModelMapExt temp= new CommonApiModelMapExt();
					BeanUtils.copyProperties(temp, jsonData);
					
					if(temp.getCode()==1){
						Map dataO=new HashMap();
						SickBaseInfo oInfo=new SickBaseInfo();
						Object o=jsonData.get("data");
						if(!(o instanceof JSONArray)){
							BeanUtils.copyProperties(oInfo, o);
							oInfoList.add(oInfo);
						}else{
							List oList=(List) o;
							for(Object obj:oList){
								oInfo=new SickBaseInfo();
								BeanUtils.copyProperties(oInfo, obj);
								oInfoList.add(oInfo);
							}
							
							//保存数组
						}
						
					}
					
//				    循环处理
					for(SickBaseInfo sbi:oInfoList){
						List<SickBaseInfo> oldObj=this.mBaseDao.findBySql("select * from  "+mBaseDao.tableName(SickBaseInfo.class)+" where 1=1   "+searchSql, SickBaseInfo.class, paramMap);
						SickBaseInfo oInfo=new SickBaseInfo();
						if(oldObj.size()>0){
							oInfo= oldObj.get(0);
							String id=oInfo.getId();
							BeanUtils.copyProperties(oInfo, sbi);
							oInfo.setId(id);
							oInfo.setUpdateTime(new Date());
							this.mBaseDao.updateObject(oInfo);
						}else{
							this.mBaseDao.saveObject(sbi);
						}
					}
					
				}
				pl =new PageList<>(oInfoList, null);
			}else{
				
				pl = this.mBaseDao.findBySqlForPageList("select * from  "+mBaseDao.tableName(SickBaseInfo.class)+" where 1=1  "+searchSql, Map.class, null, paramMap);
			}
			/**
			 * 请求http数据api
			 */
			
			mm.put(CommonApiModelMapExt.ERRCODE, 0);
			mm.put(CommonApiModelMapExt.ERRMSG, "查询成功");
			mm.put("data", pl.getResultList());
			return mm;
			
		} catch (Exception e) {
			e.printStackTrace();
			mm.put(CommonApiModelMapExt.ERRCODE, 1);
			mm.put(CommonApiModelMapExt.ERRMSG, "系统异常");
		}
		
		return mm;
	}
	@ApiOperation(value = "查询患者的检查信息", notes = "查询患者的检查信息-说明")
	@GetMapping("/getSickCheckInfo/{type}/{token}")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",dataType="number",name="type",value="请求端（ 0:web,1:android,2:iphone）",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="token",value="用户令牌",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="bedNO",value="患者床号",required=true),
		@ApiImplicitParam(paramType="query",dataType="string",name="sickAreaNO",value="床位病区号",required=true)
	})
	@GlobalLogAnnotation(logType=2,discription="查询患者的检查信息")
	public Map getSickCheckInfo(@PathVariable("type") Integer type, @PathVariable("token") String token,@RequestParam(required=true,name="bedNO")String bedNO,@RequestParam(required=true,name="sickAreaNO")String sickAreaNO) {
		CommonApiModelMapExt mm = chekTokenCahceValid(token, type);
		try {
			if (!isTokenValid) {
				return mm;
			}
			
			Map paramMap = new HashMap<>();
			String searchSql = "";
			if (!ChkUtil.isEmpty(bedNO)) {
				searchSql += " and bedNO = #{pojo.bedNO} ";
				paramMap.put("bedNO",  bedNO);
			}
			if (!ChkUtil.isEmpty(sickAreaNO)) {
				searchSql += " and sickAreaNO = #{pojo.sickAreaNO} ";
				paramMap.put("sickAreaNO",  sickAreaNO);
			}
			
//			searchSql += " order by inHosTime desc";//默认入院时间倒序
			PageList pl=new PageList<>();
//			查询job接口是否异常， 如果异常 则只取本地数据库
			this.isHisErr(TxbyBDApiConfig.class.getName());
			List<SickBaseInfo> oInfoList=new ArrayList<>();
			if(propertySourceConfig.getIsExecuteHisHttp()!=null&&propertySourceConfig.getIsExecuteHisHttp().equals("true")&&!isHisErr){
				List<TxbyBDApiConfig> bdInfoList=this.getTxbyBDApiConfigInfo(TxbyBDApiConfig.class.getName());
				if(!ChkUtil.isEmptyAllObject(bdInfoList)){
					TxbyBDApiConfig bdInfo=bdInfoList.get(0);
					Map jo=JSON.parseObject(bdInfo.getApiParam());
					jo.put("bedNO", bedNO);
					jo.put("sickNO", bedNO);
//					{"bedNO": "$bedNO", "sickNO": "$sickNO"}
					String json=this.getHttpResponseContent(bdInfo.getApiBaseUrl()+bdInfo.getApiServletPath(), "get",jo);
					JSONObject jsonData=JSON.parseObject(json);
					CommonApiModelMapExt temp= new CommonApiModelMapExt();
					BeanUtils.copyProperties(temp, jsonData);
					
					if(temp.getCode()==1){
						Map dataO=new HashMap();
						SickBaseInfo oInfo=new SickBaseInfo();
						Object o=jsonData.get("data");
						if(!(o instanceof JSONArray)){
							BeanUtils.copyProperties(oInfo, o);
							oInfoList.add(oInfo);
						}else{
							List oList=(List) o;
							for(Object obj:oList){
								oInfo=new SickBaseInfo();
								BeanUtils.copyProperties(oInfo, obj);
								oInfoList.add(oInfo);
							}
							
							//保存数组
						}
						
					}
					
//				    循环处理
					for(SickBaseInfo sbi:oInfoList){
						List<SickBaseInfo> oldObj=this.mBaseDao.findBySql("select * from  "+mBaseDao.tableName(SickBaseInfo.class)+" where 1=1   "+searchSql, SickBaseInfo.class, paramMap);
						SickBaseInfo oInfo=new SickBaseInfo();
						if(oldObj.size()>0){
							oInfo= oldObj.get(0);
							String id=oInfo.getId();
							BeanUtils.copyProperties(oInfo, sbi);
							oInfo.setId(id);
							oInfo.setUpdateTime(new Date());
							this.mBaseDao.updateObject(oInfo);
						}else{
							this.mBaseDao.saveObject(sbi);
						}
					}
					
				}
				pl =new PageList<>(oInfoList, null);
			}else{
				
				pl = this.mBaseDao.findBySqlForPageList("select * from  "+mBaseDao.tableName(SickBaseInfo.class)+" where 1=1  "+searchSql, Map.class, null, paramMap);
			}
			/**
			 * 请求http数据api
			 */
			
			mm.put(CommonApiModelMapExt.ERRCODE, 0);
			mm.put(CommonApiModelMapExt.ERRMSG, "查询成功");
			mm.put("data", pl.getResultList());
			return mm;
			
		} catch (Exception e) {
			e.printStackTrace();
			mm.put(CommonApiModelMapExt.ERRCODE, 1);
			mm.put(CommonApiModelMapExt.ERRMSG, "系统异常");
		}
		
		return mm;
	}
	@GetMapping("/getSick")
	@AuthIgnore
	public Map getSickCheckIndddfo() {
	 System.out.println("ddddd");
		
		return null;
	}


}